// Copyright (c) 2024, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:stream_channel/stream_channel.dart';

/// Starts an WebSocket server that waits for the peer to send a Close frame.
void hybridMain(StreamChannel<Object?> channel) async {
  late HttpServer server;

  server = (await HttpServer.bind('localhost', 0))
    ..listen((request) async {
      final webSocket = await WebSocketTransformer.upgrade(
        request,
      );

      webSocket.listen((event) {
        channel.sink.add(event);
      }, onDone: () {
        webSocket.close(4123, 'server closed the connection');
        channel.sink.add(webSocket.closeCode);
        channel.sink.add(webSocket.closeReason);
      });
    });

  channel.sink.add(server.port);
  await channel
      .stream.first; // Any writes indicates that the server should exit.
  unawaited(server.close());
}
